#include "StdAfx.h"
#include "Light.h"

namespace Renderer
{

	Light::Light()
	{
		ZeroMemory(&light, sizeof(D3DLIGHT9));
		SetType(D3DLIGHT_POINT);
		Move(0.0f, 0.0f, 0.0f);
		SetDiffuseColor(255, 255, 255);
		SetAmbientColor(255, 255, 255);
		SetRange(1000.f);
		SetAttenuation0(1.0f);
	}

	D3DLIGHT9* Light::GetLight()
	{
		return &light;
	}

	void Light::SetType(D3DLIGHTTYPE type)
	{
		light.Type = type;
	}

	void Light::Move(float posX, float posY, float posZ)
	{
		light.Position.x = posX;
		light.Position.y = posY;
		light.Position.z = posZ;
	}
	void Light::MoveRel(float posX, float posY, float posZ)
	{
		light.Position.x += posX;
		light.Position.y += posY;
		light.Position.z += posZ;
	}
	void Light::GetPos(float *posX, float *posY, float *posZ)
	{
		if(posX != NULL)
			*posX = light.Position.x;

		if(posY != NULL)
			*posY = light.Position.y;

		if(posZ != NULL)
			*posZ = light.Position.z;
	}

	void Light::Point(float fromX, float fromY, float fromZ,
		float atX, float atY, float atZ)
	{
		Move(fromX, fromY, fromZ);

		light.Direction.x = atX - fromX;
		light.Direction.y = atY - fromY;
		light.Direction.z = atZ - fromZ;
	}

	void Light::GetDirection(float *dirX, float *dirY, float *dirZ)
	{
		if(dirX != NULL)
			*dirX = light.Direction.x;

		if(dirY != NULL)
			*dirY = light.Direction.y;

		if(dirZ != NULL)
			*dirZ = light.Direction.z;
	}

	void Light::SetDiffuseColor(BYTE red, BYTE green, BYTE blue)
	{
		light.Diffuse.r = 1.0f / 255.0f * (float)red;
		light.Diffuse.g = 1.0f / 255.0f * (float)green;
		light.Diffuse.b = 1.0f / 255.0f * (float)blue;
	}

	void Light::GetDiffuseColor(BYTE *red, BYTE *green, BYTE *blue) const
	{
		if(red != NULL)
			*red = (char)(255.0f * light.Diffuse.r);

		if(green != NULL)
			*green = (char)(255.0f * light.Diffuse.g);

		if(blue != NULL)
			*blue = (char)(255.0f * light.Diffuse.b);
	}

	void Light::SetSpecularColor(BYTE red, BYTE green, BYTE blue)
	{
		light.Specular.r = 1.0f / 255.0f * (float)red;
		light.Specular.g = 1.0f / 255.0f * (float)green;
		light.Specular.b = 1.0f / 255.0f * (float)blue;
	}
	void Light::GetSpecularColor(BYTE *red, BYTE *green, BYTE *blue) const
	{
		if(red != NULL)
			*red = (char)(255.0f * light.Specular.r);

		if(green != NULL)
			*green = (char)(255.0f * light.Specular.g);

		if(blue != NULL)
			*blue = (char)(255.0f * light.Specular.b);
	}

	void Light::SetAmbientColor(BYTE red, BYTE green, BYTE blue)
	{
		light.Ambient.r = 1.0f / 255.0f * (float)red;
		light.Ambient.g = 1.0f / 255.0f * (float)green;
		light.Ambient.b = 1.0f / 255.0f * (float)blue;
	}

	void Light::GetAmbientColor(BYTE *red, BYTE *green, BYTE *blue)
	{
		if(red != NULL)
			*red = (char)(255.0f * light.Ambient.r);

		if(green != NULL)
			*green = (char)(255.0f * light.Ambient.g);

		if(blue != NULL)
			*blue = (char)(255.0f * light.Ambient.b);
	}

	void Light::SetRange(float range)
	{
		light.Range = range;
	}

	float Light::GetRange() const
	{
		return light.Range;
	}

	void Light::SetFalloff(float falloff)
	{
		light.Falloff = falloff;
	}

	float Light::GetFalloff() const
	{
		return light.Falloff;
	}

	void Light::SetAttenuation0(float attenuation)
	{
		light.Attenuation0 = attenuation;
	}

	float Light::GetAttenuation0() const
	{
		return light.Attenuation0;
	}

	void Light::SetAttenuation1(float attenuation)
	{
		light.Attenuation1 = attenuation;
	}

	float Light::GetAttenuation1() const
	{
		return light.Attenuation1;
	}

	void Light::SetAttenuation2(float attenuation)
	{
		light.Attenuation2 = attenuation;
	}

	float Light::GetAttenuation2() const
	{
		return light.Attenuation2;
	}

	void Light::SetTheta(float theta)
	{
		light.Theta = theta;
	}

	float Light::GetTheta() const
	{
		return light.Theta;
	}

	void Light::SetPhi(float phi)
	{
		light.Phi = phi;
	}

	float Light::GetPhi() const
	{
		return light.Phi;
	}

}